﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DataProviders;
using System.Collections.Specialized;
using System.Collections;

namespace SCRUM.Core.DAO
{
    /// <summary>
    /// Data access object for task.
    /// </summary>
    public static class DAOTask
    {
        /// <summary>
        /// Gets all tasks.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns>List of tasks for specified user.</returns>
        public static List<Task> GetAllUsersTasks(int userID)
        {
            return ProviderFactory.Provider.GetAllUsersTasks(userID);
        }

        /// <summary>
        /// Creates the task.
        /// </summary>
        /// <param name="task">The specified task.</param>
        /// <param name="bliId">Bli of task</param>
        /// <returns>Created Task</returns>
        public static Task CreateTask(Task task, int bliId)
        {
            return ProviderFactory.Provider.CreateTask(task, bliId);
        }

        /// <summary>
        /// Updates the task.
        /// </summary>
        /// <param name="task">The specified task.</param>
        /// <param name="bliId">Bli of task</param>
        public static void UpdateTask(Task task, int bliId)
        {
            ProviderFactory.Provider.UpdateTask(task, bliId);
        }

        /// <summary>
        /// Deletes the task.
        /// </summary>
        /// <param name="taskID">The task ID.</param>
        public static void DeleteTask(int taskID)
        {
            ProviderFactory.Provider.DeleteTask(taskID);
        }

        /// <summary>
        /// Gets the task by ID.
        /// </summary>
        /// <param name="taskID">The task ID.</param>
        /// <returns>Task with specified ID.</returns>
        public static Task GetTaskByID(int taskID)
        {
            return ProviderFactory.Provider.GetTaskByID(taskID);
        }

        /// Gets all Tasks for bli
        /// </summary>
        /// <param name="bli">Backlog item</param>
        /// <returns>All tasks in bli</returns>
        public static List<Task> GetAllTasksForBli(BLI bli)
        {
            return ProviderFactory.Provider.GetAllTasksForBli(bli.ID);
        }

        /// Gets all Tasks for bli ID
        /// </summary>
        /// <param name="bliID">Backlog item</param>
        /// <returns>All tasks in bli</returns>
        public static List<Task> GetAllTasksForBli(int bliID)
        {
            return ProviderFactory.Provider.GetAllTasksForBli(bliID);
        }

        /// <summary>
        /// Gets all tasks for bli in the state.
        /// </summary>
        /// <param name="bli">The bli.</param>
        /// <param name="stateID">The state ID.</param>
        /// <returns></returns>
        public static IList<Task> GetAllTasks(BLI bli, int stateID)
        {
            return ProviderFactory.Provider.GetAllTasks(bli, stateID);
        }

        /// <summary>
        /// Gets all tasks.
        /// </summary>
        /// <param name="projectID">The project ID.</param>
        /// <param name="releaseID">The release ID.</param>
        /// <param name="sprintID">The sprint ID.</param>
        /// <param name="userStoryID">The user story ID.</param>
        /// <param name="bliID">The bli ID.</param>
        /// <param name="taskID">The task ID.</param>
        /// <returns></returns>
        public static IList<Task> GetAllTasks(int projectID, int releaseID, int sprintID, int userStoryID, int bliID, int taskID)
        {
            return ProviderFactory.Provider.GetAllTasks(projectID, releaseID, sprintID, userStoryID, bliID, taskID);
        }

        /// <summary>
        /// Updates the state of the task.
        /// </summary>
        /// <param name="taskID">The task ID.</param>
        /// <param name="state">The svnstate.</param>
        public static void UpdateTaskState(int taskID, string state)
        {
             // Initialization of the SVN states mapping from config
            StringDictionary svnTaskStates = new StringDictionary();
            svnTaskStates.Add(Configuration.ConfigManager.CurrentConfig.SVNStates.Implementation, "implementation");
            svnTaskStates.Add(Configuration.ConfigManager.CurrentConfig.SVNStates.Proposed, "proposed");
            svnTaskStates.Add(Configuration.ConfigManager.CurrentConfig.SVNStates.Test, "test");
            svnTaskStates.Add(Configuration.ConfigManager.CurrentConfig.SVNStates.Review, "review");
            svnTaskStates.Add(Configuration.ConfigManager.CurrentConfig.SVNStates.Fixed, "fix");
            svnTaskStates.Add(Configuration.ConfigManager.CurrentConfig.SVNStates.Closed, "closed");

            if (svnTaskStates.ContainsKey(state))
            {
                ProviderFactory.Provider.UpdateTaskState(taskID, svnTaskStates[state]);
            }
            else
            {
                throw new SCRUM.Core.Exceptions.SCRUMException("Invalid SVN state");
            }
        }

        /// <summary>
        /// Removes associated user from the task.
        /// </summary>
        /// <param name="task">The task.</param>
        /// <param name="userID">The login.</param>
        public static void RemoveAssociatedUser(Task task, int userID)
        {
            ProviderFactory.Provider.RemoveAssociatedUser(task.ID, userID);
        }

        /// <summary>
        /// Adds the associated user.
        /// </summary>
        /// <param name="task">The task.</param>
        /// <param name="userID">The user ID.</param>
        public static void AddAssociatedUser(Task task, int userID)
        {
            ProviderFactory.Provider.AddAssociatedUser(task.ID, userID);
        }
    }
}